package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.Login;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TLog;
import cn.com.ddapp.ddm.util.TextUtil;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;
import cn.com.ddapp.ddm.widget.TipsToast;
import okhttp3.Call;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2016/7/29 0029.
 */
public class ForgetPwdActivity extends BaseActivity {

	public static String LOGIN_EMAIL = "login_email";

	@BindView(R.id.get_code)
	TextView get_code;
	@BindView(R.id.email_register)
	EditText email;
	@BindView(R.id.verification_code)
	EditText verification_code;

	TimeCount timeCount;

	@Override
	public void initView() {

		setTitle(R.string.forget_password1);
		setTitleLeftImg(R.mipmap.back_icon);
		TextUtil.setFlags(get_code);

	}

	@Override
	public void initData() {

	}

	@Override
	public int getContentViewId() {
		return R.layout.forgetpwd_main;
	}

	@OnClick({R.id.get_code, R.id.next_btn})
	void onclick(View v) {

		switch (v.getId()) {
			//获取验证码
			case R.id.get_code:
				if (!NetUtils.isConnected(ForgetPwdActivity.this)) {
					TipsToast.makeText(ForgetPwdActivity.this, R.string.tip_network_error, TipsToast.LENGTH_SHORT).show();
				} else {
					if (!TextUtils.isEmpty(email.getText().toString().trim())) {
						final OkUtilCallback okUtilCallback = new OkUtilCallback() {
							@Override
							public void onMyResponse(String response, int id) {
								TLog.log(getFragmentTag(), response);
								Entity entity = gson.fromJson(response, Entity.class);
								if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
								} else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
									BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
								} else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
									timeCount.start();
								}
							}

							@Override
							public void onError(Call call, Exception e, int id) {
								super.onError(call, e, id);
								BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
							}
						};
						OKUtilAPI.post(OKUtilAPI.getCode, "key=" + email.getText().toString(), okUtilCallback);
					} else {
						TipsToast.makeText(ForgetPwdActivity.this, R.string.email_is_null, TipsToast.LENGTH_SHORT).show();
					}
				}
				break;
			//下一步
			case R.id.next_btn:

				if (!NetUtils.isConnected(ForgetPwdActivity.this)) {
					TipsToast.makeText(ForgetPwdActivity.this, R.string.tip_network_error, TipsToast.LENGTH_SHORT).show();
				} else {

					if (TextUtils.isEmpty(email.getText().toString().trim())) {
						TipsToast.makeText(ForgetPwdActivity.this, R.string.email_is_null, TipsToast.LENGTH_SHORT).show();
						return;
					}
					if (TextUtils.isEmpty(verification_code.getText().toString().trim())) {
						TipsToast.makeText(ForgetPwdActivity.this, R.string.verification_code_is_null, TipsToast.LENGTH_SHORT).show();
						return;
					}
					final OkUtilCallback okUtilCallback = new OkUtilCallback() {
						@Override
						public void onMyResponse(String response, int id) {
							TLog.log(getFragmentTag(), response);
							Entity entity = gson.fromJson(response, Entity.class);
							if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
							} else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
								BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
							} else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
								Intent intent = new Intent(ForgetPwdActivity.this, PwdAgainActivity.class);
								intent.putExtra(LOGIN_EMAIL,email.getText().toString());
								startActivity(intent);
							}
						}

						@Override
						public void onError(Call call, Exception e, int id) {
							super.onError(call, e, id);
							BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
						}
					};
					OKUtilAPI.post(OKUtilAPI.chkCode, "key=" + email.getText().toString() + "&randCode=" + verification_code.getText().toString(), okUtilCallback);
				}
				break;

		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		timeCount = new TimeCount(60000, 1000, get_code);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		timeCount.cancel();
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}
}
